package com.rio.core;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import java.util.TreeMap;

import com.rio.core.L;
import com.rio.core.U;

/**
 * 主要所有字段必须是 public 字符串
 * 
 * @author rio
 * 
 */
public abstract class Api {

	private TreeMap<String, String> params = null;

	protected TreeMap<String, String> getParams() {
		if (U.isNull(params)) {
			params = new TreeMap<String, String>(new Comparator<Object>() {
				public int compare(Object o1, Object o2) {
					if (o1 == null || o2 == null)
						return 0;
					else
						return String.valueOf(o1).compareTo(String.valueOf(o2));
				}
			});
		}
		return params;
	}

	/**
	 * 设置成员变量拼接为接口参数对
	 */
	public TreeMap<String, String> getFieldParam() {

		Field[] child = this.getClass().getFields();
		if (null != child && child.length > 0) {
			try {
				for (Field f : child) {
					setParam(f.getName(), f.get(this));
				}
			} catch (Exception e) {
				L.e(e);
			}
		}
		return getParams();
	}

	public Api setParam(String key, Object object) {
		if (U.isNull(key) || U.isNull(object)) {

		} else {
			if ((object instanceof String)) {
				getParams().put(key.trim(), String.valueOf((String) object));
				return this;
			}
			if ((object instanceof Integer)) {
				getParams().put(key.trim(), String.valueOf((Integer) object));
				return this;
			}
			if ((object instanceof Long)) {
				getParams().put(key.trim(), String.valueOf((Long) object));
				return this;
			}
			if ((object instanceof Float)) {
				getParams().put(key.trim(), String.valueOf((Float) object));
				return this;
			}
			if ((object instanceof Boolean)) {
				getParams().put(key.trim(), String.valueOf((Boolean) object));
				return this;
			}
			if ((object instanceof Double)) {
				getParams().put(key.trim(), String.valueOf((Double) object));
				return this;
			}
		}

		return this;
	}

	public Api setParam(String key, int num) {
		if (U.isNull(key)) {

		} else {
			getParams().put(key.trim(), U.toString(num));
		}
		return this;
	}

	public Api setParam(String key, long num) {
		if (U.isNull(key)) {

		} else {
			getParams().put(key.trim(), U.toString(num));
		}
		return this;
	}

	public Api setParam(String key, String value) {
		if (U.isNull(key) || U.isNull(value)) {

		} else {
			getParams().put(key.trim(), value.trim());
		}
		return this;
	}

	public Api removeParam(String key) {
		if (U.isNull(key) || !getParams().containsKey(key)) {

		} else {
			getParams().remove(key);
		}
		return this;
	}

	protected String getRequest(TreeMap<String, String> map) {
		String reqURL = S.EMPTY;
		if (U.isNull(map)) {
			map = getParams();
		}
		if (!map.isEmpty()) {
			StringBuilder urlBuilder = new StringBuilder();
			Iterator iter = map.entrySet().iterator();
			Map.Entry<String, String> entry = null;
			String key = null;
			String val = null;
			try {
				while (iter.hasNext()) {
					entry = (Entry<String, String>) iter.next();
					key = entry.getKey();
					val = entry.getValue();
					urlBuilder.append(S.AND).append(key).append(S.EQUAL)
							.append(URLEncoder.encode(val, "utf-8"));
				}
			} catch (Exception e) {
				L.e(e);
			}

			reqURL = urlBuilder.toString();
			if (!U.isNull(reqURL)) {
				reqURL = reqURL.replaceFirst(S.AND, S.QUESTION);
			}
			map.clear();
		}
		params = null;// 清空
		return getHost() + reqURL;
	};

	/**
	 * 加上参数的URL
	 * 
	 * @return
	 */
	public String getRequest() {
		return getRequest(getFieldParam());
	};

	/**
	 * 接口URL
	 * 
	 * @return
	 */
	public abstract String getHost();

	/**
	 * 设置Content-Type
	 * 
	 * @return
	 */
	public abstract String getContentType();

}
